/**
 * 产品修改js
 */
var comValidform = null;
var returnStatus = false;
var productInfoUpdate = {
		dataCache : null,
		introduceUE : null,
		incomeUE : null,
		riskctrlUE : null,
		signflowUE:null,
		key : null,
		companyId:null,
	bindEvent : function() {
		
		//设置发行方
		// 添加供应商列表
		var url = 'mgr/companyCtrl/list';
		var params = {
			'curPage' : 0,
			'pageSize' : 9999,
			'id' : null,
			'name' : null
		}
		
		var cb = function(data) {
			if (data.resultCode == 200) {
				var option = '<option value="">请选择发行方</option>';
				for (var i = 0; i < data.result.length; i++) {
					var singleObj = data.result[i];
					option = option + '<option value="' + singleObj.id + '">'
							+ singleObj.name + '</option>';
				}
				$('select[name="issuerId"]').append(option);
			}
		};
		sendApiNoLoadSync(url, params, cb, 'POST');
		
		 
	},
	startEvent : function() {
		$("#recordTab ul").idTabs();
		productInfoUpdate.introduceUE = UE.getEditor('introduce', {
			initialFrameHeight : 340,
			initialFrameWidth : 898
		});
		productInfoUpdate.incomeUE = UE.getEditor('income', {
			initialFrameHeight : 340,
			initialFrameWidth : 898
		});
		productInfoUpdate.riskctrlUE = UE.getEditor('riskctrl', {
			initialFrameHeight : 340,
			initialFrameWidth : 898
		});
		
		productInfoUpdate.signflowUE = UE.getEditor('signflow', {
			initialFrameHeight : 340,
			initialFrameWidth : 898
		});

		productInfoUpdate.showInfoById();

		// 控制贴息选择
		$('.com-addForm select[name="discount"]').change(function() {
			var selVal = $(this).val();
			if (selVal == 1) {
				$('#txTr').css('display', '');
				$("input[name='discountRate']").attr('type', 'text');
				$("input[name='discountRate']").attr('ignore', '');
				$("input[name='discountEndDate']").attr('type', 'text');
				$("input[name='discountEndDate']").attr('ignore', '');
			} else {
				$('#txTr').css('display', 'none');
				$("input[name='discountRate']").attr('type', 'hidden');
				$("input[name='discountRate']").attr('ignore', 'ignore');
				$("input[name='discountRate']").val(null);
				$("input[name='discountEndDate']").val(null);
				$("input[name='discountEndDate']").attr('type', 'hidden');
				$("input[name='discountEndDate']").attr('ignore', 'ignore');
			}
		});
		comValidform = $(".com-addForm").Validform(
				{
					datatype : {
						"m0-2" : /^[0-9]+(.[0-9]{1,2})?$/
					},
					tiptype : 2,
					tipSweep : true,
					beforeSubmit : function(curform) {
						var url = 'mgr/ProductInfoCtrl/updateProductInfo';
						var params = jsUtils.serializeJson($('.com-addForm'));
						var introduce = productInfoUpdate.introduceUE
								.getContent();
						params.introduce = introduce;
						var income = productInfoUpdate.incomeUE.getContent();
						params.income = income;
						
						var riskctrl = productInfoUpdate.riskctrlUE
								.getContent();
						params.riskctrl = riskctrl;
						
						//如何签约
						var signflow = productInfoUpdate.signflowUE
						.getContent();
						params.signflow = signflow;
						
						if (params.discount != 1) {
							delete params.discountDate,
									delete params.discountBeginDate;
							delete params.discountEndDate;
							delete params.discountRate;
						}
						//更改日期类型
						params.proTerm = params.proTerm
								* $("select[name='proTermType']").val();
						delete params.proTermType;
						var cb = function(data) {
							alert(data.msg);
							// 成功返回
							if (data.resultCode == 200) {
								localStorage.removeItem(productInfoUpdate.key);
								// 关闭窗口并刷新数据
								art.dialog.close();
								returnStatus = true;
							}
						};
						sendApiNoLoadSync(url, params, cb, 'POST');
						return false;
					}
				});
	},

	showInfoById : function() {
		 
		var id = art.dialog.data('updId');
		productInfoUpdate.key = id + "_ProductType03_Key";

		var ret = eval('(' + localStorage.getItem(productInfoUpdate.key) + ')');
		 
		 
		for ( var i in ret) {
			var text = ret[i];
			if (i == 'establishDate' || i == 'onlineDate' || i == 'endDate'
					|| i == 'releasesDate' || i == 'balanceDate'
					|| i == 'discountDate'
					|| i == 'raiseBeginDate' || i == 'raiseEndDate'
					|| i == 'discountEndDate') {
				text = text == null ? '' : (new Date(text))
						.format('yyyy-MM-dd');
			} else if (i == 'issuerId' || i == 'prodType' || i == 'publishType'
					|| i == 'riskLevel' || i == 'currency' || i == 'isOversea'
					|| i == 'discount' || i == 'discountRate' ||i=='directional') {

				if (i == 'discount' && text == 1) {
					$('#txTr').css('display', '');
					$("input[name='discountRate']").attr('type', 'text');
					$("input[name='discountRate']").attr('ignore', '');
					$("input[name='discountEndDate']").attr('type', 'text');
					$("input[name='discountEndDate']").attr('ignore', '');
				}
				
				if(i == 'issuerId'){
					 
					$('#com-addForm select[name="' + i + '"]').val(1);
				}
				$('#com-addForm select[name="' + i + '"]').val(text);
				continue;
			} else if (i == 'claimRecommendInfo' || i == 'creditAssignmentInfo'
					|| i == 'paymentSource' || i == 'remark' || i == 'fundUse'
					|| i == 'incomeDisType' || i == 'salePoint'
					|| i == 'commission') {
				$('#com-addForm textarea[name="' + i + '"]').val(text);
				continue;
			} else if (i == 'proTerm') {
				//设置投资期限
				if (text > 360) {
					text = text / 360;
					$('#com-addForm select[name=proTermType]').val(360);
				} else if (text > 30) {
					text = text / 30;
					$('#com-addForm select[name=proTermType]').val(30);
				} else {
					text = text;
					$('#com-addForm select[name=proTermType]').val(1);
				}
			}else if(i == 'clearDate'){
                text = text == null ? '' : (new Date(text)).format('yyyy-MM-dd hh:mm:ss');
			}
			$('#com-addForm input[name="' + i + '"]').val(text);
		}
		if (ret.introduceTxt != null) {
			productInfoUpdate.introduceUE.ready(function() {
				productInfoUpdate.introduceUE.setContent(ret.introduceTxt);
			});
		}
		if (ret.incomeTxt != null) {
			productInfoUpdate.incomeUE.ready(function() {
				productInfoUpdate.incomeUE.setContent(ret.incomeTxt);
			});
		}
		if (ret.riskctrlTxt != null) {
			productInfoUpdate.riskctrlUE.ready(function() {
				productInfoUpdate.riskctrlUE.setContent(ret.riskctrlTxt);
			});
		}
		
		if (ret.signflowTxt != null) {
			productInfoUpdate.signflowUE.ready(function() {
				productInfoUpdate.signflowUE.setContent(ret.signflowTxt);
			});
		}
	},

	init : function() {
		productInfoUpdate.bindEvent();
		productInfoUpdate.startEvent();
	}
}
$(document).ready(function() {
	productInfoUpdate.init();
});
function submitForm() {
	comValidform.submitForm();
	return returnStatus;
}